﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Portal.Cms.Render;
using Portal.Web.Model;
using Portal.Cms.Bussiness;
using Portal.Web.Utility;
using Portal.Cms.Model;

namespace Portal.Cms.Render
{
    public class CollectionDataRenderBll
    {
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut Draw(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                string GroupDataId = (string)WebEnvironments.Request(SiteId, "gid");
                GroupDataCls OGroupData = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).CreateGroupDataModel(SiteId, GroupDataId);
                if (OGroupData == null)
                {
                    throw new Exception("Tham số truy cập không hợp lệ!");
                }
                PublicGroupDataFolderCls[] PublicGroupDataFolders = PublicGroupDataFolderProcessBll.ReadingPublicGroupDataFolder(SiteId, GroupDataId, null);
                string Html =
                   " <script language=javascript>\r\n" +
                   "    function ClientAdd()\r\n" +
                   "    {\r\n" +
                   "        window.open('AddPublicFolderData.aspx?gid=" + GroupDataId + "','_self');\r\n" +
                   "    }\r\n" +
                   "    function ClientUpdate(pgid)\r\n" +
                   "    {\r\n" +
                   "        window.open('UpdatePublicFolderData.aspx?gid=" + GroupDataId + "&pgid='+pgid,'_self');\r\n" +
                   "    }\r\n" +
                   "    function ClientDeleteCollection(Id)\r\n" +
                   "    {\r\n" +
                   "        SiteId='"+SiteId+"';\r\n"+
                   "        if(confirm('Bạn đã chắc chắn xóa chưa?')==false)return;\r\n" +
                   "        AjaxOut = Portal.Cms.Render.CollectionDataRenderBll.ServerSideDeletePublicFolder(SiteId,'"+GroupDataId+"',Id).value;\r\n"+
                   "        if(AjaxOut.Error)\r\n"+
                   "        {\r\n"+
                   "            alert(AjaxOut.InfoMessage);\r\n"+
                   "            return;\r\n"+
                   "        }\r\n"+
                   "        document.getElementById('divListItems').innerHTML=AjaxOut.HtmlContent;\r\n"+
                   "    }\r\n" +
                   "    function ClientUpdateCollection(Id)\r\n" +
                   "    {\r\n" +
                   "        SiteId='"+SiteId+"';\r\n"+
                   "        window.open('UpdatePublicFolderData.aspx?gid="+GroupDataId+"&pgid='+Id,'_self');\r\n"+
                   "    }\r\n" +
                   " </script>\r\n" +
                   " <div class=FormFunction id=divListForm>\r\n" +
                   "    <div class=DatabaseFunction>\r\n" +
                   "        <ul>\r\n" +
                   "            <li onclick=\"javascript:ClientAdd();\"><img class=image src=\"Resources/Images/Add.png\"><div>Tạo mới</div></li>\r\n" +
                   "            <li onclick=\"javascript:window.open('DocumentInputDataTreeView.aspx?gid="+GroupDataId+"','_self');\"><img class=image src=\"Resources/Images/back.png\"><div>Quay lại</div></li>\r\n" +
                   "        </ul>\r\n" +
                   "    </div>\r\n" +
                   "    <div id=\"divListItems\">\r\n"+
                            ServerSideReading(SiteId, GroupDataId).HtmlContent+
                   "    </div>\r\n"+
                   "</div>\r\n";
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideReading(string SiteId,string GroupDataId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                GroupDataCls OGroupData = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).CreateGroupDataModel(SiteId, GroupDataId);
                if (OGroupData == null)
                {
                    throw new Exception("Tham số truy cập không hợp lệ!");
                }
                PublicGroupDataFolderCls[] PublicGroupDataFolders = PublicGroupDataFolderProcessBll.ReadingPublicGroupDataFolder(SiteId, GroupDataId, null);
                string Html =
                   "  <h3>Danh sách các thư mục dữ liệu</h3>\r\n";
                if (PublicGroupDataFolders.Length == 0)
                {
                    Html += "<div class=CssNoData>Chưa có thư mục dữ liệu nào</div>\r\n";
                }
                else
                {
                    Html += "<div class=CssListCollection>\r\n" +
                            "   <ul>\r\n";
                    for (int iIndex = 0; iIndex < PublicGroupDataFolders.Length; iIndex++)
                    {
                        Html += " <li><img title=\"" + PublicGroupDataFolders[iIndex].Description + "\" alt=\"" + PublicGroupDataFolders[iIndex].Description + "\" src=\"Resources/Images/Collection.png\" class=image><div  title=\"" + PublicGroupDataFolders[iIndex].Description + "\" alt=\"" + PublicGroupDataFolders[iIndex].Description + "\"><a href=\"PublicDocumentInputDataTreeView.aspx?gid=" + GroupDataId + "&folder=" + PublicGroupDataFolders[iIndex].PublicGroupDataFolderId+ "\">" + PublicGroupDataFolders[iIndex].FolderName + "</a></div><div><img alt=\"Xóa thư mục tài liệu\" title=\"Xóa thư mục tài liệu\" class=\"CssImageDelete\" src=\"Resources/Images/Delete.png\" onclick=\"javascript:ClientDeleteCollection('" + PublicGroupDataFolders[iIndex].PublicGroupDataFolderId + "');\"><img alt=\"Sửa thư mục tài liệu\" title=\"Sửa thư mục tài liệu\" class=\"CssImageDelete\" src=\"Resources/Images/Update.png\" onclick=\"javascript:ClientUpdateCollection('" + PublicGroupDataFolders[iIndex].PublicGroupDataFolderId + "');\"></div></li>\r\n";
                    }
                    Html +=
                        "</ul>\r\n" +
                        "</div>\r\n";
                }

                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideDeletePublicFolder(
            string SiteId,
            string GroupDataId,
            string PublicFolderId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                PublicGroupDataFolderProcessBll.DeletePublicGroupDataFolder(SiteId, PublicFolderId);
                OAjaxOut.HtmlContent = ServerSideReading(SiteId, GroupDataId).HtmlContent;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

    }
}
